package TUILayer;
import java.util.Scanner;
import java.util.ArrayList;

import ModelLayer.*;
import ControlLayer.*;

/**
 * Write a description of class LoanUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanUI
{
    // instance variables - replace the example below with your own
    private LoanCtr loanCtr;
    private MainMenuUI mmu;

    /**
     * Constructor for objects of class LoanUI
     */
    public LoanUI()
    {
        // initialise instance variables
        loanCtr = new LoanCtr();
        mmu = new MainMenuUI();
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public void start()
    {
        loanMenu();
    }
    
    public void loanMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeLoanMenu(); 
          if(choice == 1)
          {   //show loan
              this.getLoan();
          }
          else{
              if(choice == 2){
                    //create loan
                    this.createLoan();
              }  
              else{
                  if(choice == 3){
                      //delete loan
                      this.deleteLoan();
                  }  
                  else{
                      if(choice == 4){
                          // update loan
                          this.updateLoan();
                    }
                      else{
                          if(choice == 5){
                              this.listAllLoans();
                          }
                          else{
                              if(choice == 6){
                                    //Return to main menu
                                         mmu.mainMenuStart();}
                                    else{
                                        exit = true;
                          }//end else
                      }//end else
                  }//end else
              }//end else
          }//end else
        }
            
       }//end while
    }
    
    public int writeLoanMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" *** Loan Menu ***");
            System.out.println(" (1) Show information about a loan");
            System.out.println(" (2) Add loan");
            System.out.println(" (3) Delete loan");
            System.out.println(" (4) Update loan");
            System.out.println(" (5) List all loans");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            
            return choice;
    }
    
    private long inputLoanID()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Loan Id:  ");
           try
           {
               id = keyboard.nextLong();
               ok = true;
           }
           catch (Exception e)
           {
               System.out.println("You must enter a number -  try again");
               String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    
    private int inputLoanPeriod()
    {   
        boolean ok = false;
        int id = 0;
        while (!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Loan Period:  ");
           try
           {
               id = keyboard.nextInt();
               ok = true;
           }
           catch (Exception e)
           {
               System.out.println("You must enter a number -  try again");
               String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    
    private String inputBorrowDate()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write BorrowDate:  ");
         String name = keyboard.nextLine();
         
         return name;
    }
    
    private String inputChoice()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" YES/NO?:  ");
         String name = keyboard.nextLine();
         if (name.equals("YES") || name.equals("NO"))
         {
             return name;
         }
         else
         {
             System.out.println("You must enter YES or NO -  try again");
             String input = keyboard.nextLine();
         }
         
         return name;
    }
   
    private long inputLoanDvdID()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Loan DVD ID:  ");
           try
           {
               id = keyboard.nextLong();
               ok = true;
           }
           catch (Exception e)
           {
               System.out.println("You must enter a number -  try again");
               String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    
    private long inputLoanCopyID()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Loan Copy ID:  ");
           try
           {
               id = keyboard.nextLong();
               ok = true;
           }
           catch (Exception e)
           {
               System.out.println("You must enter a number -  try again");
               String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    
    private long inputLoanPersonID()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Person ID:  ");
           try
           {
               id = keyboard.nextLong();
               ok = true;
           }
           catch (Exception e){
               System.out.println("You must enter a number -  try again");
               String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    
    private void getLoan()
    {
        loanCtr.getLoan(this.inputLoanID());
    }
    
    private void createLoan()
    {
        long personID = 0;
        long dvdID = 0;
        long copyID = 0;
        ArrayList<DVD> d = new ArrayList<DVD>();
        d = loanCtr.listAllDvds();
        ArrayList<Copy> c = new ArrayList<Copy>();
        ArrayList<Person> p = new ArrayList<Person>();
        p = loanCtr.listAllPerson();
        if (p!=null && d!=null)
        {
            System.out.println("Choose a Person:");
            for (int i=0;i<p.size();i++)
            {
                System.out.println("Name: " + p.get(i).getName() + " ID: " + p.get(i).getPersonID());
            }
            personID=this.inputLoanPersonID();
            System.out.println("Chose a Copy of a DVD:");
            for (int i=0; i<d.size(); i++)
            {
                System.out.println("Title: " + d.get(i).getTitle() + " ID: " + d.get(i).getId());
                c = loanCtr.listAllCopies(d.get(i).getId());
                for (int j=0; j<c.size(); j++)
                {
                    System.out.println("-Serial Number: " + c.get(j).getSerialNumber() + " Available: " + c.get(j).getAvailability());
                }
            }
            dvdID=this.inputLoanDvdID();
            copyID=this.inputLoanCopyID();
            if (DVDCtr.getCopy(dvdID, copyID).getAvailability() == false)
            {
                System.out.println("This Copy is not available at the moment! Would you like to reserve it?");
                if (this.inputChoice().equals("YES"))
                {
                    loanCtr.createLoan(this.inputLoanID(), this.inputBorrowDate(), this.inputLoanPeriod(), personID, dvdID, copyID);
                }
                else
                {
                    this.createLoan();
                }
            }
            else
            {
                loanCtr.createLoan(this.inputLoanID(), this.inputBorrowDate(), this.inputLoanPeriod(), personID, dvdID, copyID);
            }
        }
        else
        {
            System.out.println("There are no people in the Address Book and/or DVDs in the collection");
        }
    }
    
    private void updateLoan()
    {
        long id = this.inputLoanID();
        if (loanCtr.getLoan(id) != null)
        {
            loanCtr.updateLoan(new Loan(id, this.inputBorrowDate(), this.inputLoanPeriod()));
        }
        else
        {
            System.out.println("This loan is not present in the system!");
        }
    }
    
    private void deleteLoan()
    {
        loanCtr.deleteLoan(this.inputLoanID());
    }
    
    private void listAllLoans()
    {
        if (loanCtr.listAllLoans() == null)
        {
            System.out.println("There are no loans in the system");
        }
        else
        {
            for (int i=0; i<loanCtr.listAllLoans().size(); i++)
            {
                System.out.println("ID: " + loanCtr.listAllLoans().get(i).getId() + "; Date: " + loanCtr.listAllLoans().get(i).getBorrowDate() + "; Period: " + loanCtr.listAllLoans().get(i).getPeriod());
            }
        }
    }
}
